<?php

namespace Home\Model;

class PrivatechatModel extends CommonModel
{
    public function checkFree($free, $money, $recharge_user, $userid): array
    {
        $UserModel = new UserModel;
        $GradeNobleModel = new GradeNobleModel;
        $GradeNobleUseModel = new GradeNobleUseModel;

        if ($recharge_user['noble_time'] > time() && $recharge_user['noble_grade']) {
            $gradeNoble = $GradeNobleModel->where(['grade' => $recharge_user['noble_grade']])->find();

            if ($gradeNoble) {
                $privilege = explode(',', $gradeNoble['privilege']);

                if (in_array(10, $privilege)) {
                    $privilegeValue = explode(',', $gradeNoble['value']);

                    if (isset($privilegeValue[0]) && $privilegeValue[0]) {
                        $userCount = $GradeNobleUseModel->where(['types' => 3, 'user_id' => $recharge_user['id'], 'create_time' => ['egt', strtotime(date('Ymd'))]])->count();

                        $privilegeValue[0] = intval($privilegeValue[0]);
                        if ($privilegeValue[0] > $userCount) {
                            $GradeNobleUseModel->add(['types' => 3, 'user_id' => $recharge_user['id'], 'create_time' => time()]);

                            $free = 1;
                        }
                    }
                }
            }
        }

        if (!$free) {
            if ($recharge_user['free_chat']) {
                $UserModel->where(['id' => $recharge_user['id']])->setDec('free_chat');
            } else {
                if ($recharge_user['volley'] < $money) {
                    if ($recharge_user['id'] == $userid) {
                        return ['code' => 300, 'msg' => '余额不足，无法发送消息'];
                    } else {
                        return ['code' => 400, 'msg' => '对方余额不足，无法发送消息'];
                    }
                }
            }
        }

        return ['code' => 200, 'data' => ['free' => $free]];
    }

    
    public function checkGreet($user_id)
    {
        $ConfigPersonalModel = new ConfigPersonalModel;
        $ChatGreetModel = new ChatGreetModel;
        $ConfigSystemModel = new ConfigSystemModel;
        $UserModel = new UserModel;
        $GradeNobleModel = new GradeNobleModel;
        
        $configPersonal = $ConfigPersonalModel->where(['user_id' => $user_id])->find();
        
        if (isset($configPersonal['fate_switch']) && intval($configPersonal['fate_switch']) == 0) {
            return ['code' => 400, 'msg' => '批量打招呼功能已关闭'];
        }
        
        // 查询用户性别
        $userxx = $UserModel->where(['id' => $user_id])->field('id, sex')->find();
        // 女性用户直接通过，不进行任何限制
        if ($userxx['sex']=='女') {
            return ['code' => 200, 'msg' => 'success'];
        }
        
        $chatGreet = $ChatGreetModel->where(['user_id' => $user_id])->find();
        
        $config = $ConfigSystemModel->where(['category' => 'sayhi'])->getField('title, content');
        
        if ($chatGreet) {
            $current_time = time();
            $update_time = $chatGreet['update_time'];

            $user = $UserModel->where(['id'=>$user_id])->find();
            if ($user['noble_grade'] && $user['noble_time'] > $current_time) {
                $privilege = $GradeNobleModel->where(['grade' => $user['noble_grade']])->getField('privilege');
                $privilege = explode(',', $privilege);
                if (in_array(14, $privilege)) {
                    $config['sayhi_times'] = $config['sayhi_times'] / 2;
                }
            }
            
            if ($chatGreet['count'] >= $config['sayhi_count']) {
                $ChatGreetModel->save(['id' => $chatGreet['id'], 'count' => 0, 'update_time' => $current_time]);
                
                return ['code' => 400, 'msg' => '批量打招呼次数暂时达到上限，' . $config['sayhi_times'] . '分钟后可再次使用', 'expire_time' => $current_time + $config['sayhi_times'] * 60];
            } else if ($update_time && ($update_time + $config['sayhi_times'] * 60) > $current_time) {
                $minutes = $config['sayhi_times'] - floor(($current_time - $update_time) / 60);
                
                return ['code' => 400, 'msg' => "批量打招呼次数暂时达到上限，{$minutes}分钟后可再次使用", 'expire_time' => $update_time + $config['sayhi_times'] * 60];
            } else {
                if ($chatGreet['update_time']) {
                    $ChatGreetModel->save(['id' => $chatGreet['id'], 'update_time' => 0]);
                }
            }
    
            $ChatGreetModel->where(['id' => $chatGreet['id']])->setInc('count');
        } else {
            $ChatGreetModel->add(['user_id' => $user_id, 'count' => 1]);
        }
        
        return ['code' => 200, 'msg' => 'success'];
    }
}
